<template>
  <el-row justify="end">
    <el-col :span="16" style="text-align: left;">
      <el-button @click="exportData" type="primary">导出数据</el-button>
    </el-col>
    <el-col :span="6">
      <el-input v-model="searchInput" placeholder="请输入内容"></el-input>
    </el-col>
    <el-col :span="2">
      <el-button type="primary" @click="searchData">搜索</el-button>
    </el-col>
  </el-row>
  <el-table
    :data="
      tableData.filter(
        (data) =>
          !search || data.name.toLowerCase().includes(search.toLowerCase())
      )
    "
    :header-cell-style="{background:'#f4f3f9',color:'#606266', fontSize: '12px'}"
    :cell-style = "{fontSize: '12px'}"
    style="width: 100%; margin-top: 10px;"
  >
    <!--
    <el-table-column
      prop="id"
      label="序号"
      width="50">
    </el-table-column>
    -->
    <el-table-column prop="id" v-if="false"> </el-table-column>
    <el-table-column label="姓名" prop="user_name"> </el-table-column>
    <el-table-column label="身份证号" prop="card_id"> </el-table-column>
    <el-table-column label="地址" prop="address"> </el-table-column>
    <el-table-column label="电话" prop="phone"> </el-table-column>
    <el-table-column label="小区" prop="subdistrict" width="80"> </el-table-column>
    <el-table-column label="楼栋" prop="building" width="80"> </el-table-column>
    <el-table-column label="单元" prop="block" width="80"> </el-table-column>
    <el-table-column label="房间号" prop="room" width="80"> </el-table-column>
    <el-table-column label="操作" align="right">
      <template #default="scope">
          <el-button @click="editUser(scope.row)" size="small">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    small="true"
    background
    layout="prev, pager, next"
    :page-size="5"
    :total="total"
    :current-page="currentPage"
    @current-change="changePage"
    style="margin-top: 30px;">
  </el-pagination>
  <el-drawer
  title="编辑"
  v-model="drawer"
  v-show="drawer"
  :lock-scroll="false"
  :with-header="true">
      <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
        <el-form-item label="id" v-show="false">
          <el-input v-model="formLabelAlign.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="formLabelAlign.userName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="formLabelAlign.cardId"></el-input>
        </el-form-item>
        <el-form-item label="户籍地">
          <el-input v-model="formLabelAlign.address"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="formLabelAlign.phone"></el-input>
        </el-form-item>
        <el-form-item label="小区名称">
          <el-input v-model="formLabelAlign.subdistrict"></el-input>
        </el-form-item>
        <el-form-item label="楼栋">
          <el-input v-model="formLabelAlign.building"></el-input>
        </el-form-item>
        <el-form-item label="单元">
          <el-input v-model="formLabelAlign.block"></el-input>
        </el-form-item>
        <el-form-item label="房间号">
          <el-input v-model="formLabelAlign.room"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
  </el-drawer>
</template>
<script>
import {getUser, updateUser, search, exportFile} from "../api/user.js"
export default {
  data() {
    return {
      tableData: [
      ],
      search: "",
      drawer: false,
      labelPosition: 'left',
      formLabelAlign: {
        userName: '', 
        cardId: '', 
        address: '', 
        phone: '', 
        subdistrict: '',
        building: '',
        block: '', 
        room: ''
      },
      queryParams: {
        page: 1,
        size: 5
      },
      total: '',
      currentPage: 1,
      searchInput: ''
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    async getUserList() {
      this.queryParams.user = localStorage.getItem("user");
      let result = await getUser(this.queryParams);
      if(result.data.status == 'ok') {
        this.tableData = result.data.userList;
        this.total = result.data.total
        console.log(result.data.total);
      }
    },
    editUser(row) {
      this.formLabelAlign.userName = row.user_name;
      this.formLabelAlign.cardId = row.card_id;
      this.formLabelAlign.address = row.address;
      this.formLabelAlign.phone = row.phone;
      this.formLabelAlign.subdistrict = row.subdistrict;
      this.formLabelAlign.building = row.building;
      this.formLabelAlign.block = row.block;
      this.formLabelAlign.room = row.room;
      this.formLabelAlign.id = row.id;
      this.drawer = true
    },
    async onSubmit() {
      let result = await updateUser(this.formLabelAlign);
      if(result.data.status == "ok") {
        this.drawer = false;
        this.getUserList();
      }
    },
    changePage(e) {
      this.currentPage = e;
      this.queryParams.page = e;
      this.getUserList();
    },
    async searchData() {
      console.log(this.searchInput);
      let result = await search({'search': this.searchInput});
      if(result.data.status == 'ok') {
        this.tableData = result.data.userList;
      }
    },
    async exportData() {
      let userName = localStorage.getItem("user");
      let result = await exportFile(userName);
      console.log(result);
    }
  },
  mounted() {
    this.getUserList();
  }
};
</script>
<style scoped>
</style>